package cn.lxb.demo.controller;

import cn.lxb.demo.domain.constant.EasyPoiPdfTemplate;
import cn.lxb.demo.domain.dto.UserInfoDTO;
import cn.lxb.demo.domain.entity.UserInfoEntity;
import cn.lxb.demo.service.UserInfoService;
import cn.lxb.web.starter.template.controller.FindController;
import cn.lxb.web.starter.template.helper.EasyPoiPdfHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * spring-boot-web-template-starter test
 *
 * @author 54lxb
 * @since 2019-11-21
 */
@Api(tags = "PDF导出")
@RestController
@RequestMapping("/pdf")
public class PdfController implements FindController<UserInfoDTO, UserInfoEntity, UserInfoService, Long> {

    @Autowired
    private UserInfoService userInfoService;

    @Override
    public UserInfoService getService() {
        return userInfoService;
    }

    @Override
    public UserInfoEntity newEntity() {
        return new UserInfoEntity();
    }

    @ApiOperation("导出PDF")
    @GetMapping("/export/demo")
    public void exportPdf(HttpServletResponse response) {
        try {
            Map<String, Object> data = new HashMap<>(3);
            data.put("title", "用户信息列表");
            List<UserInfoEntity> users = getService().selectList(null);
            List<UserInfoDTO> results = users.stream().map(UserInfoEntity::toDTO).collect(Collectors.toList());
            data.put("results", results);
            EasyPoiPdfHelper.createTransversePdf(data, EasyPoiPdfTemplate.DEMO, "demo", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
